<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/static/include/taglib.jsp"%>

<!DOCTYPE html>
<html>
<head>
	<title>商品管理</title>
	<%@ include file="/static/include/head.jsp" %>
</head>
<body>
<!-- 模态框（Modal） -->
<form id="inputForm" modelAttribute="goods" action="${ctx}/product/goods/save" method="post" class="form-horizontal">
<div class="tabs-container">
    <ul class="nav nav-tabs">
        <li class="active">
            <a data-toggle="tab" href="#tab-1" aria-expanded="true">基本信息</a>
        </li>
        <li>
            <a data-toggle="tab" href="#tab-2" aria-expanded="false">相册</a>
        </li>
        <li>
            <a data-toggle="tab" href="#tab-3" aria-expanded="false">规格</a>
        </li>
        <li>
            <a data-toggle="tab" href="#tab-4" aria-expanded="false">属性</a>
        </li>
        <li>
            <a data-toggle="tab" href="#tab-5" aria-expanded="false">参数</a>
        </li>
    </ul>
    
    <!-- error message here -->
    <div id="messageBox" class="alert alert-warning hide"></div>
    <tags:message content="${message}"/>
    
    <div class="tab-content">
    	<div id="tab-1" class="tab-pane fade in active">
             <%@ include file="goodsBaseForm.jsp" %>
         </div>
         <div id="tab-2" class="tab-pane fade">
             <%@ include file="goodsPhotoForm.jsp" %>
         </div>
         <div id="tab-3" class="tab-pane fade">
             <%@ include file="goodsSpecForm.jsp" %>
         </div>
         <div id="tab-4" class="tab-pane fade">
             <%@ include file="goodsPropsForm.jsp" %>
         </div>
         <div id="tab-5" class="tab-pane fade">
             <%@ include file="goodsParamsForm.jsp" %>
         </div>
    </div>
    <div class="modal-footer">
			<button type="button" class="btn btn-default" onclick="javascript:returnList();">返回</button>
            <c:if test="${param.type ne 'view'}">
			<button type="submit" class="btn btn-primary" onclick="javascript:;">提交更改</button>
			</c:if>
	</div>
</div>
</form>
<script type="text/javascript">
$(function(){
	$("input[type=text]").attr("autocomplete", "off");
	
	$("#inputForm").validate({
        submitHandler: function(form){
        	//如果商品开启了规格，就必须添加规格，否则无法保存
			if($("#haveSpec").val() == 1){
				if($("#spec-input .detail .spec_table tbody tr").length == 0){
					alert("您已开启规格，但是还未添加规格，请添加后再保存！",function(){
						$('a[data-toggle="tab"][href="#tab-3"]').tab('show');
					});
					return false;
				}
			} 
			//图片上传判定
			if(typeof($("input[type='hidden'][name='goodsPhotos']").val()) == "undefined"){
				alert("图片必须上传！",function(){
					$('a[data-toggle="tab"][href="#tab-2"]').tab('show');
				});
				return false;
			}

            form.submit();
        },
        errorContainer: "#messageBox",
        errorPlacement: function(error, element) {
            $("#messageBox").html('<div class="alert alert-danger">' +
				'<a href="#" class="close" data-dismiss="alert">&times;</a>' +
				'<strong>警告！</strong>输入有误，请进行一些更改。</div>');
            if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
                error.appendTo(element.parent().parent());
            } else {
                error.insertAfter(element);
            }
        }
    });
});

function returnList(){
 	window.location.href = "${ctx}/product/goods";
}
</script>
</body>
</html>
